package com.aaa.service.fpa.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.dao.fpa.ViewResultFpaDao;
import com.aaa.entity.fpa.ResultDiv;
import com.aaa.entity.fpa.TestPlanFpa;
import com.aaa.entity.fpa.ViewTestResultFpa;
import com.aaa.entity.mht.TestResultMht;
import com.aaa.entity.system.PageOrder;
import com.aaa.service.fpa.ViewResultService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：姚建军（2511037015@qq.com）
 * @date ：：Created in 2022/9/5 16:47
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ViewResultServiceImpl implements ViewResultService {
    @Autowired
    ViewResultFpaDao viewResultFpaDao;

    /**
     * 查询及分页
     * @param pageOrder
     * @param viewResultFpa
     * @return
     */
    @Override
    public Page queryAll(PageOrder pageOrder, ViewTestResultFpa viewResultFpa) {
        PageHelper.startPage(pageOrder.getPage(), pageOrder.getLimit());
        if (ObjectUtil.isNotEmpty(pageOrder.getField())) {
            PageHelper.orderBy(pageOrder.getFieldOrder());
        }
        List<ViewTestResultFpa> list = viewResultFpaDao.queryAll(viewResultFpa);
        Page pageResult = (Page) list;
        return pageResult;
    }

    /**
     * 删除结果表
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(int[] id) {
        return viewResultFpaDao.deleteById(id);
    }

    /**
     * 删除测试者表
     * @param id
     * @return
     */
    @Override
    public boolean deleteByTesterId(int[] id) {
        return viewResultFpaDao.deleteByTesterId(id);
    }

    /**
     * 按照id查询
     * @return
     */
    @Override
    public List<ViewTestResultFpa> queryTestPlanId() {
        return viewResultFpaDao.queryTestPlanId();
    }

    /**
     *查询性格结果表
     * @param id
     * @return
     */
    @Override
    public List<ResultDiv> queryAllResultDiv(Integer id) {
        return viewResultFpaDao.queryAllDiv(id);
    }

    /**
     * 查询结果表的性格颜色
     * @return
     */
    @Override
    public Map<String, Integer> queryAllPlanFpa() {
        List<ViewTestResultFpa> fpas = viewResultFpaDao.queryAllPlanFpa();
        int r = 0, y = 0, b = 0, g = 0, f = 0;
        for (ViewTestResultFpa fpa : fpas) {
            int red = fpa.getRedCount();
            int yellow = fpa.getYellowCount();
            int blue = fpa.getBlueCount();
            Long green = fpa.getGreenCount();
            if (red > yellow && red > blue && red > green) {
                r++;
            } else if (yellow > red && yellow > blue && yellow > green) {
                y++;
            } else if (blue > red && blue > yellow && blue > green) {
                b++;
            } else if (green > red && green > yellow && green > blue) {
                g++;
            } else {
                f++;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("r", r);
        map.put("y", y);
        map.put("b", b);
        map.put("g", g);
        map.put("f", f);
        return map;
    }

    @Override
    public List<ViewTestResultFpa> queryResult(int id, String type) {
        return viewResultFpaDao.queryResult(id);
    }

    @Override
    public Page<Map<String, Object>> getByTesterId(PageOrder pageOrder, Integer testerId) {
        //拿出pageOrder里面的所有数据
        PageHelper.startPage(pageOrder.getPage(), pageOrder.getLimit());
        //判断数据是否为空
        if (ObjectUtil.isNotEmpty(pageOrder.getField())) {
            PageHelper.orderBy(pageOrder.getFieldOrder());
        }
        return (Page<Map<String,Object>>) viewResultFpaDao.getByTesterId(testerId);
    }

    /**
     * 查询id
     * @return
     */
    @Override
    public List<TestPlanFpa> queryAllResultFpa() {
        List<TestPlanFpa> testPlanFpa = viewResultFpaDao.queryAllResultPlanFpa();
        return testPlanFpa;
    }
}
